#ifndef USERINFODIALOG_H
#define USERINFODIALOG_H

#include "ElaContentDialog.h"
#include "ElaText.h"
#include "ElaLineEdit.h"
#include "ElaComboBox.h"
#include <QVBoxLayout>
#include <QFormLayout>
#include <QDebug>

class UserInfoDialog : public ElaContentDialog
{
    Q_OBJECT
  public:
    UserInfoDialog(QWidget* parent) : ElaContentDialog(parent)
    {
        setWindowTitle("用户信息");

        auto* contentWidget = new QWidget();
        auto* mainLayout = new QVBoxLayout(contentWidget);

        auto* title = new ElaText("编辑用户信息", this);
        title->setTextStyle(ElaTextType::Title);
        mainLayout->addWidget(title);
        mainLayout->addSpacing(20);

        // 表单布局
        auto* formLayout = new QFormLayout();

        m_nameEdit = new ElaLineEdit(this);
        m_nameEdit->setPlaceholderText("请输入姓名");

        m_emailEdit = new ElaLineEdit(this);
        m_emailEdit->setPlaceholderText("请输入邮箱");

        m_departmentCombo = new ElaComboBox(this);
        m_departmentCombo->addItems({"技术部", "市场部", "人事部", "财务部"});

        m_roleCombo = new ElaComboBox(this);
        m_roleCombo->addItems({"普通用户", "管理员", "超级管理员"});

        formLayout->addRow("姓名:", m_nameEdit);
        formLayout->addRow("邮箱:", m_emailEdit);
        formLayout->addRow("部门:", m_departmentCombo);
        formLayout->addRow("角色:", m_roleCombo);

        mainLayout->addLayout(formLayout);
        mainLayout->addStretch();

        setCentralWidget(contentWidget);

        // 设置按钮文本
        setLeftButtonText("取消");
        setMiddleButtonText("清空");
        setRightButtonText("保存");

        resize(500, 300);
    }

    QString getName() const
    {
        return m_nameEdit->text();
    }
    QString getEmail() const
    {
        return m_emailEdit->text();
    }
    QString getDepartment() const
    {
        return m_departmentCombo->currentText();
    }
    QString getRole() const
    {
        return m_roleCombo->currentText();
    }

  protected:
    void onLeftButtonClicked() override
    {
        qDebug() << "取消编辑用户信息";
    }

    void onMiddleButtonClicked() override
    {
        // 清空表单
        m_nameEdit->clear();
        m_emailEdit->clear();
        m_departmentCombo->setCurrentIndex(0);
        m_roleCombo->setCurrentIndex(0);
        qDebug() << "表单已清空";
    }

    void onRightButtonClicked() override
    {
        if (m_nameEdit->text().isEmpty())
        {
            qDebug() << "请输入姓名";
            return;
        }

        qDebug() << "保存用户信息:"
                 << "姓名:" << getName() << "邮箱:" << getEmail() << "部门:" << getDepartment() << "角色:" << getRole();
    }

  private:
    ElaLineEdit* m_nameEdit;
    ElaLineEdit* m_emailEdit;
    ElaComboBox* m_departmentCombo;
    ElaComboBox* m_roleCombo;
};

#endif  // USERINFODIALOG_H